package com.xsmart.goods.dao;

import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
public interface AttributeDao {

    @Select("SELECT a.attr_id as attr_id,g.cat_name,a.cat_id as cat_id,a.attr_name as attr_name," +
            "a.attr_input_type as attr_input_type,a.attr_type as attr_type,a.attr_index as attr_index," +
            "a.sort_order as sort_order,a.is_linked as is_linked " +
            " from attribute a " +
            "left join goods_type g on g.cat_id=a.cat_id" +
            " where a.cat_id = #{catId} order by sort_order asc ")
    List<Map<String,Object>> queryTypeAttr(Pagination page,Integer catId);

    @Select("SELECT a.attr_id as attr_id,a.attr_name as attr_name,attr_input_type as attr_input_type " +
            "from attribute a where a.cat_id = #{cat_id}")
    List<Map<String,Object>> queryAll(Integer cat_id);

    @Select("SELECT b.attr_value_id as attr_value_id,b.attr_value as attr_value,b.sort_order as sort_order" +
            " from attribute_values b where b.attr_id = #{attr_id} ORDER BY sort_order ")
    List<Map<String,Object>> queryAttrVal(Long attr_id);

    @Select("SELECT max(attr_id) FROM attribute" )
    Integer queryMaxId();

    @Insert("insert into attribute " +
            "(attr_id," +
            "cat_id, " +
            "attr_name," +
            "attr_input_type," +
            "attr_type," +
            "attr_index," +
            "sort_order," +
            "is_linked )" +
            "values (" +
            "#{attrMap.attr_id}," +
            "#{attrMap.cat_id}," +
            "#{attrMap.attr_name}, " +
            "#{attrMap.attr_input_type}, " +
            "#{attrMap.attr_type}, " +
            "#{attrMap.attr_index}, " +
            "#{attrMap.sort_order}, " +
            "#{attrMap.is_linked})")
    int addAttr(@Param("attrMap")Map attrMap);

    @Insert("<script>" +
            "insert into attribute_values " +
            "(attr_id, " +
            "attr_value )" +
            "values " +
            "<foreach collection=\"attrList\" item=\"item\" index=\"index\" separator=\",\" > " +
            "   (#{attr_id},#{item})" +
            "</foreach>" +
            "</script>")
    int addAttrVal(@Param("attrList")List attrList,@Param("attr_id")Integer attr_id);

    @Insert("update attribute " +
            "set " +
            "cat_id = #{attrMap.cat_id}, " +
            "attr_name = #{attrMap.attr_name}," +
            "attr_input_type = #{attrMap.attr_input_type}," +
            "attr_type = #{attrMap.attr_type}," +
            "attr_index = #{attrMap.attr_index}," +
            "sort_order = #{attrMap.sort_order}," +
            "is_linked = #{attrMap.is_linked} " +
            "where attr_id = #{attrMap.attr_id}")
    int modAttr(@Param("attrMap") Map attrMap);

    @Delete("DELETE FROM attribute WHERE attr_id=#{attr_id}")
    int delAttr(Integer attr_id);

    @Delete("DELETE FROM attribute_values WHERE attr_id=#{attr_id}")
    int delAttrVal(Integer attr_id);

    /**
     * 查询商品类型的id和名称
     * @return
     */
    @Select("select cat_id,cat_name from goods_type")
    List<Map<String,Object>> queryGoodsTypeName();

    //    @Select("SELECT a.attr_id as attr_id,a.cat_id as cat_id,a.attr_name as attr_name," +
//            "a.attr_input_type as attr_input_type,a.attr_type as attr_type,a.attr_index as attr_index," +
//            "a.sort_order as sort_order,a.is_linked as is_linked," +
//            "b.attr_value_id as attr_value_id,b.attr_value as attr_value,b.sort_order as sort_order" +
//            " from attribute a,attribute_values b where a.attr_id = b.attr_id and a.cat_id = #{cat_id}")
//    List<Map<String,Object>> queryTypeAttr(Pagination page, Integer cat_id);


}
